package com.qs.controller;


import com.qs.model.dto.GoodsDetailDto;
import com.qs.model.dto.GoodsIndexDto;
import com.qs.model.es.EsGoodsDto;
import com.qs.response.Response;
import com.qs.service.IGoodsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 商品接口
 * @author author
 * @since 2024-03-23
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequestMapping("/goods")
@RequiredArgsConstructor
public class GoodsController {

    private final IGoodsService goodsService;

    /**
     * 获取首页商品轮播图片
     * @return
     */
    @GetMapping("/index/image")
    public Response getIndexImage(){
        log.info("首页获取轮播图请求.........");
        Map<String,Object> images =  goodsService.findIndexImage();
        log.info("首页获取轮播图请求成功.........");
        return Response.success(images);
    }

    /**
     * 获取首页热卖商品
     * @return
     */
    @GetMapping("/index/hot")
    public Response getIndexHotGoods(){
        log.info("获取首页热卖商品请求.........");
        List<GoodsIndexDto> list =  goodsService.findIndexHotGoods();
        log.info("获取首页热卖商品请求成功.........");
        return Response.success(list);
    }

    /**
     * 获取首页猜你喜欢商品
     * @return
     */
    @GetMapping("/index/recommend")
    public Response getIndexRecommendGoods(){
        log.info("获取首页猜你喜欢商品请求.........");
        List list =  goodsService.findRecommendGoods();
        log.info("获取首页猜你喜欢商品请求成功.........");
        return Response.success(list);
    }

    /**
     * 首页商品搜索结果返回接口
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/search/{ids}/{keyword}")
    public Response searchGoods(@PathVariable("ids") List<Integer> ids,
                                @PathVariable("keyword") String keyword) throws IOException {
        log.info("首页商品搜索结果返回接口请求进来：ids：{},keyword：{}------",ids,keyword);
        List<GoodsIndexDto>  searchGoods = goodsService.searchFrontList(ids,keyword);
        log.info("首页商品搜索结果返回接口请求成功：ids：{},keyword：{}------",ids,keyword);
        return Response.success(searchGoods);
    }

    /**
     * 前台搜索接口
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/search/{param}")
    public Response search(@PathVariable("param") String param) throws IOException {
        log.info("前台搜索接口请求进来------搜索词为：{}",param);
        List<Integer> goodsIds = goodsService.frontSearchList(param);
        log.info("前台搜索接口请求成功------搜索词为：{}",param);
        return Response.success(goodsIds);
    }

    /**
     * 商品详情页
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/detail/{id}")
    public Response goodsDetail(@PathVariable("id") Integer id){
        log.info("商品详情页请求进来------商品id为：{}",id);
        GoodsDetailDto result = goodsService.findGoodsDetail(id);
        log.info("商品详情页请求成功------商品id为：{}",id);
        return Response.success(result);
    }
}
